const path = require('path')
const uglufy = require('uglifyjs-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const rootPath = process.cwd()
const fs = require("fs")
const srcPath = path.join(rootPath, 'src')
const baseConfig = {
  entry: {
    common: path.join(srcPath, 'index.js')
  },
  output: {
    filename: "static/js/bundle.[hash].js",
    publicPath: '/'
  },
  resolve: {
    alias: {
      '@': path.join(srcPath, 'images')
    }
  },
  module: {
    loaders: [
      {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader',
              options: {
                minimize: true
              }
            },
            {
              loader: 'postcss-loader',
              options: {
                config: {
                  path: rootPath + '/postcss.config.js'
                }
              }
            },
            {
              loader: 'less-loader'
            }
          ]
        })
      },
      {
        test: /\.(png|jpg|gif)/,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 20000,
              outputPath: 'static/images/'
            }
          }
        ]
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modles/,
        query: {
          presets: ['es2015', 'stage-0'],
          compact: false
        }
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('static/css/app.[hash].css')
  ],
}

function setEntrys(conf) {
  const entryPath = path.join(srcPath, 'entry')
}

function setHtmlPlugin(conf) {
  const htmlPath = path.join(srcPath, 'html')
  var files = fs.readdirSync(htmlPath)
  files.map(function(item) {
    var obj = {
      template: path.join(htmlPath, item),
      filename: item,
      inject: 'head'
    }
    conf.plugins.push(new HtmlWebpackPlugin(obj))
  })
}
setHtmlPlugin(baseConfig)
console.log('baseConfig', baseConfig)
module.exports = baseConfig
